package com.example.login.controller;

import com.example.login.biz.YongHuBiz;
import org.example.bean.QuanxianInfo;
import org.example.bean.YongHuInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/yonghu")
public class YongHuController {
    @Autowired
    private YongHuBiz yhb;
    @RequestMapping(value = "/find")
    public String findYongHuByMima(String username, String password, HttpSession session) {
        YongHuInfo yhi = yhb.findYongHuByMima(username, password);
        if (yhi==null){
            return "redirect:/qtlogin.html";
        }else {
            session.setAttribute("YONGHU_XINXI",yhi);
            return "redirect:/index.htm";
        }
    }
    @RequestMapping("/save")
    @ResponseBody
    public void saveYongHu(YongHuInfo yhi) {
        yhb.saveYongHu(yhi);
    }
    @RequestMapping(value = "/findYongHu",produces = {"application/json;charset=utf-8"})
    @ResponseBody
    public Map<String,Object> findAllYongHu(@RequestParam(defaultValue = "1") int page) {
        return yhb.findAllYongHu(page,5);
    }
    @RequestMapping(value = "/QuanXian",produces = {"application/json;charset=utf-8"})
    @ResponseBody
    public List<QuanxianInfo> findAllQuanXian() {
        return yhb.findAllQuanXian();
    }


    @RequestMapping("/id")
    @ResponseBody
    public void yongHuId(int id,HttpSession session){
        session.setAttribute("YONGHU_ID",id);
    }
    @RequestMapping("/update")
    @ResponseBody
    public void UpdateYongHU(YongHuInfo yhi,HttpSession session) {
        int id = (int)session.getAttribute("YONGHU_ID");
        yhi.setYongHuId(id);
        yhb.UpdateYongHU(yhi);
    }
    @RequestMapping("/delete")
    @ResponseBody
    public void deleteYongHU(int yongHuId) {
        yhb.deleteYongHU(yongHuId);
    }

}
